#include "server.hpp"

#define HOST "127.0.0.1"
#define PORT 3306
#define USER "root"
#define PASS "123456"
#define DBNAME "gobang"

void logger_test()
{
    ILOG("system started");
    DLOG("user %s logged in", "Alice");
    ELOG("failed to open file %s", "config.txt");
}

void mysql_test()
{
    user_table ut("127.0.0.1", "root", "123456", "gobang_test", 3306);
    // 插入测试
    Json::Value new_user;
    new_user["username"] = "tbx";
    new_user["password"] = "123456";
    ut.insert(new_user);
    // 登录测试
    Json::Value login_user;
    login_user["username"] = "tbx";
    login_user["password"] = "123456";
    ut.login(login_user);
    // 按名字查找
    Json::Value query_user;
    ut.select_by_name("tbx", query_user);
    // 更新测试
    uint64_t uid = login_user["id"].asUInt64();
    ut.win(uid);
    ut.lose(uid);
    // 检查更新
    Json::Value updated_user;
    if (ut.select_by_id(uid, updated_user))
    {
        ILOG("After updates: score=%d, win_count=%d, total=%d",
             updated_user["score"].asInt(),
             updated_user["win_count"].asInt(),
             updated_user["total_count"].asInt());
    }
}

void room_test()
{
    user_table ut("127.0.0.1", "root", "123456", "gobang_test", 3306);
    online_manager om;
    room_manager rm(&ut, &om);
    matcher mt(&rm, &ut, &om);
    uint64_t uid1 = 1;
    uint64_t uid2 = 2;
    DLOG("添加玩家1进匹配队列");
    mt.add(uid1);
    DLOG("添加玩家2进匹配队列");
    mt.add(uid2);

    room_ptr r1 = rm.get_room_by_uid(uid1);
    room_ptr r2 = rm.get_room_by_uid(uid2);

    // 服务端尚未创建，所以匹配房间一定失败
    if (r1 && r2 && r1 == r2)
    {
        DLOG("匹配成功！玩家 %lu 与 %lu 被分配到同一房间 rid=%lu", uid1, uid2, r1->id());
    }
    else
    {
        DLOG("匹配失败：无法找到有效房间或两玩家不在同一房间");
    }
}

int main()
{
    // room_test();
    gobang_server _server(HOST, USER, PASS, DBNAME, PORT);
    _server.start(8085);
    return 0;
}